package cn.breeze.org.controller;

import cn.breeze.basic.annotation.PreAuthorize;
import cn.breeze.basic.exception.BusinessException;
import cn.breeze.basic.utils.ExcelUtil;
import cn.breeze.org.domain.ShopAuditLog;
import cn.breeze.org.service.IShopService;
import cn.breeze.org.domain.Shop;
import cn.breeze.org.query.ShopQuery;
import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.Console;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存店铺", sn = "shop:save")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除店铺", sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取店铺", sn = "shop:get")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "获取所有店铺", sn = "shop:loadAll")
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "列表店铺", sn = "shop:list")
    public PageList<Shop> queryData(@RequestBody ShopQuery query)
    {
        return shopService.queryData(query);
    }

    /**
    * @Title: settlement
    * @Description: 店铺入驻
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/2 17:06
    * @Parameters: [shop]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/settlement")
    @PreAuthorize(name = "店铺入驻", sn = "shop:settlement")
    public AjaxResult settlement(@RequestBody Shop shop)
    {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: settlement
    * @Description: 审核通过
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/2 17:07
    * @Parameters: [shop]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/audit/pass")
    @PreAuthorize(name = "店铺审核通过", sn = "shop:audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog)
    {
        try {
            shopService.auditPass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: settlement
    * @Description: 审核驳回
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/2 17:08
    * @Parameters: [shop]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/audit/reject")
    @PreAuthorize(name = "店铺审核驳回", sn = "shop:audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog)
    {
        try {
            shopService.auditReject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: auditFail
    * @Description: 审核失败
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/2 23:01
    * @Parameters: [shopAuditLog]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/audit/fail")
    @PreAuthorize(name = "店铺审核失败", sn = "shop:audit/fail")
    public AjaxResult auditFail(@RequestBody ShopAuditLog shopAuditLog)
    {
        try {
            shopService.auditFail(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: useActivation
    * @Description: 店铺修改
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/3 12:52
    * @Parameters: [id]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @GetMapping("/use/activation/{id}")
    @PreAuthorize(name = "店铺使用激活", sn = "shop:use/activation")
    public AjaxResult useActivation(@PathVariable("id")Long id)
    {
        try {
            shopService.useActivation(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: exportExcel
    * @Description: excel导出
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/3 12:52
    * @Parameters: [response]
    * @Return void
    */
    @GetMapping("/export/excel")
    @PreAuthorize(name = "店铺导出", sn = "shop:export/excel")
    public void exportExcel(HttpServletResponse response)
    {
        //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
        //                                   HttpServletResponse response
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息.xlsx", response);
    }

    /**
    * @Title: importExcel
    * @Description: excel导入
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/3 12:53
    * @Parameters: [file]
    * @Return void
    */
    @PostMapping("/import/excel")
    @PreAuthorize(name = "店铺导入", sn = "shop:import/excel")
    public void importExcel(@RequestPart(value = "file", required = true)MultipartFile file)
    {
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.patchAdd(shops);
    }

    /**
    * @Title: reportShop
    * @Description: 店铺报表
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/3 14:30
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @GetMapping("/report/shop")
    @PreAuthorize(name = "店铺报表", sn = "shop:report/shop")
    public Map<String, Object> reportShop()
    {
        Map<String, Object> map = shopService.reportShop();
        return map;
    }
}
